package com.jdyfy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jdyfy.entity.Role;
import com.jdyfy.entity.User;
import com.jdyfy.result.Result;
import com.jdyfy.service.RoleService;
import com.jdyfy.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/jdyfy")
@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;



    //添加或修改角色
    @PostMapping("/role/save")
    public boolean addOrUpdateProject(@RequestBody Role role) {
        return roleService.saveOrUpdate(role);
    }

    //分页查询角色
    @GetMapping("/rolePage")
    public Result<IPage<Role>> getRolePage(@RequestParam Integer pageNum,
                                              @RequestParam Integer pageSize) {
        //创建分页对象
        IPage<Role> page = new Page<>(pageNum, pageSize);
        //创建查询对象
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();

        //按序号倒叙排序
        //queryWrapper.orderByDesc("role_id");

        //获取当前用户信息
        User currentUser = TokenUtils.getCurrentUser();
        System.out.println("6666666666666666" + currentUser.getUsername());

        //执行查询
        IPage<Role> IPage = roleService.page(page, queryWrapper);
        return Result.success(IPage);
    }

    //删除角色
    @DeleteMapping("/role/del")
    public int deleteById(@RequestBody Long id) {
        return roleService.delById(id);
    }

    /**
     * 绑定角色和菜单的关系
     * @param roleId 角色id
     * @param menuIds 菜单id数组
     * @return
     */
    @PostMapping("/roleMenu/{roleId}")
    public Result<List<Long>> roleMenu(@PathVariable Long roleId,@RequestBody List<Long> menuIds) {
        roleService.setRoleMenu(roleId, menuIds);
        return Result.success(menuIds);
    }

    /**
     * 根据角色id查询菜单id
     * @param roleId
     * @return
     */
    @PostMapping("/getRoleMenu/{roleId}")
    public Result<List<Long>> getRoleMenu(@PathVariable Long roleId) {

        return Result.success(roleService.getRoleMenu(roleId));
    }

}
